<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Data_poster extends CI_Model {

	function post_data($table,$post,$update=null)
	{
		if($update==null)
			$this->db->insert($table,$post);
		else
		{
			$this->db
				 ->where('intId',$update)
				 ->update($table,$post);
		}
	}
    
    function updateCodeUsed($code)
    {
        $code_array = $this->db->get_where('tb_mas_codes',array('strCode'=>$code,'boolUsed'=>0))->result_array();
        if(empty($code_array))
            return false;
        else{
            $this->db
             ->where('strCode',$code)
             ->update('tb_mas_codes',array('boolUsed'=>1));
            
            return true;
        }
    }
    
    function log_action($category,$action,$color)
    {
        $data = array('strCategory'=>$category,'strAction'=>$action,'dteLogDate'=>date("Y-m-d H:i:s"),'intFacultyID'=>$this->session->userdata('intID'),'strColor'=>$color);
        $this->post_data('tb_mas_logs',$data);
        
    }
    
    function update_classlist($table,$post,$update=null)
	{
		if($update==null)
			$this->db->insert($table,$post);
		else
		{
			$this->db
				 ->where('intCSID',$update)
				 ->update($table,$post);
		}
	}
    
    function set_ay_inactive()
    {
        $post['enumStatus'] = "inactive";
        $this->db->update('tb_mas_sy',$post);
    }
	
	function update_image($post,$update=null,$field = 'strFileName')
	{
		if($update!=null)
		{
			$this->db
				 ->where($field,$update)
				 ->update('tb_sys_media',$post);
			
			return 1;
		}
		return 0;
	}
		
	function delete_data($table,$id)
	{
		$this->db
			->where('intID',$id)
			->delete($table);
	}
    
    function deleteStudentCS($table,$id)
	{
		$this->db
			->where('intCSID',$id)
			->delete($table);
	}
    
    function deleteStudent($id)
	{
		$this->db
			->where('intID',$id)
			->delete('tb_mas_users');
        
        $this->db
			->where('intStudentID',$id)
			->delete('tb_mas_classlist_student');
	}
    
    function deleteClassList($id)
    {
        $this->db
			->where('intClassListID',$id)
			->delete('tb_mas_classlist_student');
        
        $this->db
			->where('intID',$id)
			->delete('tb_mas_classlist');
    }
    
    function deleteSubject($id)
    {
        
        $cl =$this->db
			->where('fk-tb_mas_subjects',$id)
            ->get('tb_mas_classlist')
            ->result_array();
        
        if(empty($cl)){
        
            $this->db
			->where('intID',$id)
			->delete('tb_mas_subjects');
        
            return true;
        }
        else
            return false;
    
    }
    
    function deleteCS($id)
	{
		$this->db
			->where('intClassListID',$id)
			->delete('tb_mas_classlist_student');
	}
	
	
}
